import axios from 'axios';
import qs from 'query-string';

export interface BizGoodsId {
    id: number;
}

export interface BizGoodsRecord {
    id: number;
    title: string;
    image_url: string;
    biz_name: string;
    price: number;
    market_price: number;
    status: number;
    created_at: string;
    desc: string;
}

export interface BizGoodsListRes {
    list: BizGoodsRecord[];
    total: number;
}

export interface BizGoodsModel {
    id: number;
    title: string;
    image: string;
    sort_by: number;
}

export interface BizGoodsParams extends Partial<BizGoodsRecord> {
    current: number;
    pageSize: number;
    title?: string;
    biz_id?: number;
}

/**
 * 获取业务列表
 * @param params
 */
export function queryBizGoodsList(params: BizGoodsParams) {
    return axios.get<BizGoodsListRes>('/admin/bizGoods/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取业务详情
 * @param params
 */
export function queryBizGoodsOne(params: BizGoodsId) {
    return axios.get<BizGoodsRecord>('/admin/bizGoods/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 提交业务表单（创建/更新）
 * @param data
 */
export function submitBizGoodsForm(data: any) {
    return axios.post('/admin/bizGoods/submit', data);
}

/**
 * 删除业务
 * @param id
 */
export function delBizGoods(id: number) {
    return axios.post('/admin/bizGoods/delete', { id });
}

/**
 * 切换商品上下架状态
 * @param id 商品ID
 */
export function toggleBizGoodsStatus(id: number) {
    return axios.post('/admin/bizGoods/toggleStatus', { id });
}
